     int[][] haha;
     int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    public  int numEnclaves(int[][] grid) {
        haha = grid;
        int n = grid.length;
        int m = grid[0].length;
        // 先找边界的1，广度都变成1
        for (int i = 0; i < n; i++) {
            if (haha[i][0] == 1) {
                bfs(i, 0);
            }
            if (haha[i][m - 1] == 1) {
                bfs(i, m - 1);
            }
        }
        for (int i = 0; i < m; i++) {
            if (haha[0][i] == 1) {
                bfs(0, i);
            }
            if (haha[n - 1][i] == 1) {
                bfs(n - 1, i);
            }
        }
        // 找到所有的1
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (haha[i][j] == 1) {
                    ans++;
                }
            }
        }
        return ans;
    }

    public  void bfs(int x, int y) {
        haha[x][y] = 0;
        Queue<Integer> queue = new LinkedList<>();
        queue.add(x * 1000 + y);
        while (!queue.isEmpty()) {
            int tmp = queue.poll();
            int px = tmp / 1000;
            int py = tmp % 1000;

            for (int[] r : direction
            ) {
                int nx = px + r[0];
                int ny = py + r[1];

                // 出界或者==0就跳过
                if (nx < 0 || nx >= haha.length || ny < 0 || ny >= haha[0].length || haha[nx][ny] == 0) {
                    continue;
                }
                queue.add(nx * 1000 + ny);
                haha[nx][ny] = 0;
            }
        }
    }